package com.second.app.thread.线程基础.day07.线程通信.管道;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

/**
 * @Author GYZ
 * @Date 2023/11/29
 * <pre>
 *
 * </pre>
 */
public class Main {
    public static void main(String[] args) {
        WriteData writeData = new WriteData();
        ReadData readData = new ReadData();

        PipedInputStream inputStream = new PipedInputStream();
        PipedOutputStream outputStream = new PipedOutputStream();

        class ThreadWrite extends Thread{
            public final PipedOutputStream pipedOutputStream;

            public final WriteData writeData;

            public ThreadWrite(PipedOutputStream pipedOutputStream,WriteData writeData){
                this.pipedOutputStream=pipedOutputStream;
                this.writeData = writeData;
            }

            @Override
            public void run() {
                writeData.writeMethod(pipedOutputStream);
            }
        }
        class ThreadRead extends Thread{
            public final PipedInputStream pipedInputStream;

            public final ReadData readData;

            public ThreadRead(PipedInputStream pipedInputStream,ReadData readData){
                this.pipedInputStream=pipedInputStream;
                this.readData = readData;
            }

            @Override
            public void run() {
                readData.readMethod(pipedInputStream);
            }
        }
        try {
            outputStream.connect(inputStream);

            ThreadRead threadRead = new ThreadRead(inputStream, readData);
            threadRead.start();

            Thread.sleep(2000);

            ThreadWrite threadWrite = new ThreadWrite(outputStream, writeData);
            threadWrite.start();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
